<?php header("Content-Type: text/xml; charset=UTF-8");
/**************************************/
/*                                    */
/*        微博输出RSS的程序            */
/*      im007boy@gmail.com提供        */
/*     http://www.neverbest.com       */
/*                                    */
/**************************************/


//define('OPEN',1);//如果希望开放给其他人用 删掉define前的//

//登录信息 从login.php获取
/*登录信息设置开始*/

$Oauth_Token = '1111111111111111111111111111';
$Oauth_Token_Secret = '1111111111111111111111111111';

/*登录信息设置结束*/

//需要读取的微博ID 
$Feed_Id = '1577476122';//我的1577476122

//输出数量 1~200
$Feed_Max = '30';

//标题 
$Feed_Title = 'im007boy的新浪微博';

//你懂得
define('AD_007',' im007boy@gmail.com 提供');

/**************************************/
/*    下面这些不要修改 否则会出错滴     */
/**************************************/
include_once( 'config.php' );
include_once( 'weibooauth.php' );

if (defined('OPEN')){
    if (array_key_exists('user_id',$_GET))
        $Feed_Id = $_GET['user_id'];
    
    if (array_key_exists('num',$_GET))
        $Feed_Max = $_GET['num'];
        
    if (array_key_exists('title',$_GET))
        $Feed_Title = $_GET['title'];
}
//RSS简介
$Feed_Description = $Feed_Title . AD_007;


if (!($Feed_Max > 0 && $Feed_Max <= 200))
    $Feed_Max = 2;

    

$this_page = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https://' : 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"];
$c = new WeiboClient( $Wb_AppKey , $Wb_AppSecret , $Oauth_Token , $Oauth_Token_Secret );
$ms = $c->user_timeline(1,$Feed_Max,$Feed_Id);
?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'. "\n"; ?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title><![CDATA[<?php echo $Feed_Title;?>]]></title>
	<link><![CDATA[<?php echo $this_page; ?>]]></link>
	<atom:link href="<?php echo htmlentities($this_page,ENT_COMPAT ,"UTF-8"); ?>" rel="self" type="application/rss+xml" />
	<description><![CDATA[<?php echo $Feed_Description;?>]]></description>
    <?php
        if (is_array($ms) && !array_key_exists('error',$ms) && count($ms,0) >= 1){
            echo '	<lastBuildDate>' . date("r",strtotime($ms[0]['created_at'])) . '</lastBuildDate>';
        }
    ?>
    
	<language>zh-cn</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/>
	<atom:link rel="hub" href="http://superfeedr.com/hubbub"/>
<?php 
	if( is_array( $ms ) )
		if (array_key_exists('error',$ms)){
			echo '<error>';
			echo '<request>' . $ms['request'] . '</request>';
			echo '<error_code>' .$ms['error_code'] . '</error_code>';
			echo '<error>' . $ms['error'] . '</error>';
			if ($ms['error_code'] == 401)
                echo '<info>登录错误 请检查Oauth_Token和Oauth_Token_Secret是否正确。且本程序的appkey必须与获取oauth_token的appkey一致</info>';
            echo '<contect>程序错误，请联系im007boy@gmail.com 谢谢。</contect>';
			echo '</error>';
		}else {
			foreach( $ms as $item ){ ?>
				<item>
				<title><![CDATA[
                    <?php echo $item['text']; ?>
                ]]></title>
				<link><![CDATA[<?php echo 'http://api.weibo.com/' . $item['user']['id'] . '/statuses/' . $item['id']; ?>]]></link>
				<comments><![CDATA[
                    <?php echo 'http://api.weibo.com/' . $item['user']['id'] . '/statuses/' . $item['id']; ?>
                ]]></comments>
				<pubDate><?php echo date("r",strtotime($item['created_at'])) ?></pubDate>
				<dc:creator><![CDATA[<?php echo $item['user']['screen_name'];?>]]></dc:creator>
				<category><![CDATA[新浪微博]]></category> 
				<guid isPermaLink="false"><![CDATA[<?php echo 'http://api.weibo.com/' . $item['user']['id'] . '/statuses/' . $item['id']; ?>]]></guid>
				<description><![CDATA[<?php echo $item['text']; ?>]]></description>
				<content:encoded><![CDATA[
					<div style="background-color:#E5ECF9;border-radius: 10px;padding: 5px;">
					<?php 
						echo '<p>' . $item['text'] . '</p>'; 
						if (array_key_exists('original_pic',$item)){
							//image
							echo '<img style="display:block;" src="' . $item['original_pic'] . '"></img>';
						}
						
						if (array_key_exists('retweeted_status',$item)){
							//retweet
							echo '<p style="margin:10px;padding:5px;border-top:1px solid #3366CC;background-color:#E5ECF9;width:90%;">';
							echo '<span>@<a target="_blank" href="http://www.weibo.com/' . $item['retweeted_status']['user']['id'] . '">' . $item['retweeted_status']['user']['screen_name'] . '</a>:</span>';
							echo $item['retweeted_status']['text'];
							if (array_key_exists('original_pic',$item['retweeted_status'])){
								//image
								echo '<img style="display:block;" src="' . $item['retweeted_status']['original_pic'] . '"></img>';
							}
							echo '</p>';
						}
					?>	
					</div>
				
				]]></content:encoded>
				</item>
		<?php		
			}
		} ?>
</channel>
</rss>